/**
 * 公共方法
 **/
var Publicethod = {}

//省市区节点扁平数组转树状数组
Publicethod.locationOrganizationalStructure = function(froum){
    for (let i in froum) {
        froum[i].value = froum[i].l_id;
        froum[i].label = froum[i].l_name;
    }
    let temp = {};
    let ans = [];
    for (let i in froum) {
        temp[froum[i].value] = froum[i];

    }
    for (let i in temp) {
        if (temp[i].l_fid != 0) {
            if (!temp[temp[i].l_fid].children) {
                temp[temp[i].l_fid].children = [];

            }
            temp[temp[i].l_fid].children.push(temp[i]);
        } else {
            ans.push(temp[i]);

        }
    }
    
    //加序号
    for(var ss=0;ss<ans.length;ss++){
	    	ans[ss].indexs = ss;
	    	
    	for(var sshi=0;sshi<ans[ss].children.length;sshi++){
    		if(ans[ss].children[sshi].children != undefined){
	    		ans[ss].children[sshi].indexs = sshi;
    			
		    	for(var squ=0;squ<ans[ss].children[sshi].children.length;squ++){
			    	ans[ss].children[sshi].children[squ].indexs = squ;
			    }
    			
    		}else{
	    		ans[ss].children[sshi].indexs = sshi;
    		}
	    }
    	
    }
    
    return ans;
}
//组织结构节点扁平数组转树状数组
Publicethod.arrayOrganizationalStructure = function(froum){
    for (let i in froum) {
        froum[i].value = froum[i].os_id;
        froum[i].label = froum[i].os_name;
    }
    let temp = {};
    let ans = [];
    for (let i in froum) {
        temp[froum[i].value] = froum[i];

    }
    for (let i in temp) {
        if (temp[i].os_fid != 0) {
            if (!temp[temp[i].os_fid].children) {
                temp[temp[i].os_fid].children = [];

            }
            temp[temp[i].os_fid].children.push(temp[i]);
        } else {
            ans.push(temp[i]);

        }
    }
    return ans;
}

//左侧菜单栏扁平数组转树状数组
Publicethod.arrayReorganization = function(froum){
    for (let i in froum) {
        froum[i].value = froum[i].m_id;
        froum[i].label = froum[i].m_name;
        froum[i].valueLabel = froum[i].m_id + ',' + froum[i].m_url;
    }
    let temp = {};
    let ans = [];
    for (let i in froum) {
        temp[froum[i].value] = froum[i];

    }
    for (let i in temp) {
        if (temp[i].m_fid != 0) {
            if (!temp[temp[i].m_fid].children) {
                temp[temp[i].m_fid].children = [];

            }
            temp[temp[i].m_fid].children.push(temp[i]);
        } else {
            ans.push(temp[i]);

        }
    }
    return ans;
}

//新建文件及文件夹
Publicethod.newlyBuild = function(params,temporaryFatherData){

    var type = ''

    var m_path = params.m_path.split(",")

    m_path.splice(0, 1);

    m_path.splice(m_path.length-1, 1);

    var string = []

    if(m_path.length > 0){

        for (var i = 0; i < temporaryFatherData.length; i++) {

            if (temporaryFatherData[i].m_id == m_path[0]) {

                string.push(temporaryFatherData[i].m_url)

            }

            if (temporaryFatherData[i].children && temporaryFatherData[i].children.length > 0) {

                for (var v = 0; v < temporaryFatherData[i].children.length; v++) {

                    if (temporaryFatherData[i].children[v].m_id == m_path[1]) {

                        string.push(temporaryFatherData[i].children[v].m_url)

                    }

                    if (temporaryFatherData[i].children[v].children && temporaryFatherData[i].children[v].children.length > 0) {

                        for (var n = 0; n < temporaryFatherData[i].children[v].children.length; n++) {

                            if (temporaryFatherData[i].children[v].children[n].m_id == m_path[2]) {

                                string.push(temporaryFatherData[i].children[v].children[n].m_url)

                            }
                        }

                    }
                }

            }

        }

    }

    if(params.m_type == 0){

        type = './client/views/systemView/'

    }else if(params.m_type == 1){

        type = './client/views/extensionView/'

    }else if(params.m_type == 2){

        type = './client/views/bussinessView/'

    }
    

    if(string.length>0){

        type +=  string.join('/') + "/"

    }

    if(params.m_level == 0){

        type = type + params.m_url

    }else if(params.m_level == 1){

        type = type + params.m_url

    }else if(params.m_level == 2){

        type = type + params.m_url + "/" + params.m_url + ".vue"

    }else if(params.m_level == 3){

        type = type + params.m_url + "/" + params.m_url + ".vue"

    }

    return type;
}

//获取当前时间
Publicethod.currentTime = function(){
    var timestamp = Date.parse(new Date()) / 1000;
    return timestamp
}

//验证电话
Publicethod.vPhone = function(str) {
    var reg = /^1[3|4|5|7|8|9][0-9]{9}$/;
    return reg.test(str);
}

//去除字符串最后一位
Publicethod.deleteLastStr = function(str) {
    return str.substr(0, str.length - 1)
}

//验证中文
Publicethod.vChinese = function(str) {
    var reg = /^[\u4E00-\u9FA5]{1,}$/;
    return reg.test(str);
}

//判断是否为空
Publicethod.vEmpty = function(str) {
	console.log(str)
	if(str == '' || str == undefined || str == null){
        return true;
    }
    var trimStr = str.replace(/(^\s*)|(\s*$)/g, "");   
    if(trimStr.length <= 0){
        return true;
    }else{
        return false;
    }
}


//获取当前年月日
Publicethod.getNowDateInfo = function(){
    var dateInfo = {};
    var date  = new Date;
    dateInfo.year  = date.getFullYear(); 
    dateInfo.month = date.getMonth() + 1;
    dateInfo.day   = date.getDate();
    return dateInfo
}

//格式化日期时间
Publicethod.dateFormat = function(format,unix) {

    var newDate = new Date(unix*1000);

    var date = {
           "M+": newDate.getMonth() + 1,
           "d+": newDate.getDate(),
           "h+": newDate.getHours(),
           "m+": newDate.getMinutes(),
           "s+": newDate.getSeconds(),
           "q+": Math.floor((newDate.getMonth() + 3) / 3),
           "S+": newDate.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
           format = format.replace(RegExp.$1, (newDate.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
           if (new RegExp("(" + k + ")").test(format)) {
                  format = format.replace(RegExp.$1, RegExp.$1.length == 1
                         ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
           }
    }
    return format;
}

//获取JS对象的长度
Publicethod.getJsonObjLength = function(jsonObj) {
    var Length = 0;
    for (var item in jsonObj) {
        Length++;
    }
    return Length;
}


//获取当前年月日
Publicethod.getTimes = function(times, state,type,newTimes) {
	var nowDate = new Date();
	var nowOneDate = new Date();
	
	var oneYear = '0';
	var oneMonth = '0';
	var oneDay = '0';
	if(times){
		nowDate = new Date(times*1000);
	}
	if(newTimes){
		nowOneDate = new Date(newTimes*1000);
		oneYear = nowOneDate.getFullYear();
		oneMonth = nowOneDate.getMonth() + 1 < 10 ? "0" + (nowOneDate.getMonth() + 1) :
			nowOneDate.getMonth() + 1;
		oneDay = nowOneDate.getDate() < 10 ? "0" + nowOneDate.getDate() : nowOneDate.getDate();
	}
	if(!type){
		type = '-';
	}
	var year = nowDate.getFullYear();
	var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) :
		nowDate.getMonth() + 1;
	var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
	var h = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
	var m = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
	var s = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
	
	if(state == 'nian') {
		var dateStr = year;
	} else if(state == 'yue') {
		var dateStr = month;
	} else if(state == 'ri') {
		var dateStr = day;
	} else if(state == 'shifen'){
		var dateStr = year + type + month + type + day + ' ' + h + ':' + m;
	} else if(state == 'cn'){
		var dateStr = year + '年' + month + '月' + day + '日';
	} else if(state == 'cha'){
		if(year-oneYear < 1){
			var dateStr = (month-oneMonth) + '月' + (day-oneDay) + '天';
		}else{
			var dateStr = (year-oneYear) + '年' + (month-oneMonth) + '月';
		}
	} else {
		var dateStr = year + type + month + type + day;
	}
	return dateStr;
}


export default Publicethod
